<%--                                                                       --%>
<%-- Copyright (c) 2003-2005 BroadVision, Inc.  All rights reserved.       --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<%
  String ctxPath = request.getContextPath();
%>

<script language="JavaScript">

  function gotoFirstPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      thisForm.currentPageNumber.value = 1;
      thisForm.submit();
    }
  }
  
  function gotoPreviousPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      var currentPageNumber = thisForm.currentPageNumber.value;
      currentPageNumber--;
      if (thisForm.entriesPerPage.value != <c:out value="${processInstanceListForm.map.entriesPerPage}"/>) {
        currentPageNumber = 1;
      }
      if (currentPageNumber > 0) {
        thisForm.currentPageNumber.value = currentPageNumber;
        thisForm.submit();
      }
    }
  }
  
  function gotoNextPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      var currentPageNumber = thisForm.currentPageNumber.value;
      currentPageNumber++;
      if (thisForm.entriesPerPage.value != <c:out value="${processInstanceListForm.map.entriesPerPage}"/>) {
        currentPageNumber = 1;
      }
      if (currentPageNumber <= thisForm.totalPageNumber.value) {
        thisForm.currentPageNumber.value = currentPageNumber;
        thisForm.submit();
      }
    }
  }
  
  function gotoLastPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      if (thisForm.entriesPerPage.value != <c:out value="${processInstanceListForm.map.entriesPerPage}"/>) {
        thisForm.currentPageNumber.value = 1;
      }
      else {
        thisForm.currentPageNumber.value = thisForm.totalPageNumber.value;
      }
      thisForm.submit();
    }
  }

  function doGo(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      if (thisForm.entriesPerPage.value != <c:out value="${processInstanceListForm.map.entriesPerPage}"/>) {
        thisForm.currentPageNumber.value = 1;
        return true;
      }
    }
    return false;
  }
  
  function checkNumberOnly(e) {
    var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
      return false;
    }
    else {
      return true;
    }
  }

  function doValidateEntriesPerPage(formName) {
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      if (thisForm.entriesPerPage == null) {
        return true;
      }
      var entriesPerPageString = thisForm.entriesPerPage.value;
      var len = entriesPerPageString.length;
      var i = 0;
      for (i = 0; i < len; i++) {
        if (entriesPerPageString.charAt(i) < '0' || entriesPerPageString.charAt(i) > '9') {
          alert("<bean-el:message key="bp.common.pagination.validation.itemsPerPage" bundle="bp.messages"/>");
          thisForm.entriesPerPage.select();
          thisForm.entriesPerPage.focus();
          return false;
        }
      }
      if (thisForm.entriesPerPage.value < 1 || thisForm.entriesPerPage.value > 2147483647) {
        alert("<bean-el:message key="bp.common.pagination.validation.itemsPerPage" bundle="bp.messages"/>");
        thisForm.entriesPerPage.select();
        thisForm.entriesPerPage.focus();
        return false;
      }
      else {
        return true;
      }
    }
    return false;
  }

</script>
  
<% 
  java.util.HashMap map = new java.util.HashMap();
  String procOid = request.getParameter("processOid"); 
  if (procOid != null && procOid.length() > 0) {
    map.put("processOid", procOid);
  }
  else {
    map.put("processOid", "-1");
  }
  pageContext.setAttribute("pagetabParam", map, PageContext.PAGE_SCOPE);
%>

<c:set var="editImg">
<img src="<%= ctxPath %>/mc/images/action/icon_edit.gif" width="15" height="15" border="0" class="actionIcon" alt="<bean-el:message key="image.alt.edit" bundle="mc.messages"/>" title="<bean-el:message key="image.alt.edit" bundle="mc.messages"/>">
</c:set>

<html-el:form action="/bp/processInstances.do" onsubmit="return doGo('processInstanceListForm');">
<html-el:hidden name="processInstanceListForm" property="processOid"/>
<html-el:hidden name="processInstanceListForm" property="currentPageNumber"/>
<html-el:hidden name="processInstanceListForm" property="totalPageNumber"/>
<html-el:hidden name="processInstanceListForm" property="lastPage"/>
<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr> 
    <td valign="top" width="100%">
      <div class="pageTitle"><c:out value="${processInstanceListView.map.name}"/></div>
      <div class="errorMessage"><html-el:errors bundle="bp.messages"/></div>
      <%@include file="/mc/common/pagetab.jsp" %>
      <logic:present name="processSubprocessPath" scope="session">
        <logic:greaterThan name="processSubprocessPath" property="processOid" value="0">
          <div class="breadcrumb">
            <html-el:link page="/bp/processSubprocessView.do" paramId="processOid" paramName="processSubprocessPath" paramProperty="processOid">
              <c:out value="${processSubprocessPath.map.processName}"/>
            </html-el:link>
            <logic:iterate id="subprocess" name="processSubprocessPath" property="subprocesses" indexId="i">
              &nbsp;<bean-el:message key="bp.process.taskSeparator" bundle="bp.messages"/>&nbsp;
              <logic:equal name="subprocess" property="lastone" value="false">
                <html-el:link page="/bp/processSubprocessTaskView.do" name="subprocess" property="parameterTaskMap">
                  <c:out value="${subprocess.map.number}"/>:
                  <c:out value="${subprocess.map.friendlyName}"/>
                </html-el:link>
                &nbsp;<bean-el:message key="bp.process.taskSeparator" bundle="bp.messages"/>&nbsp;
                <html-el:link page="/bp/processSubprocessView.do" name="subprocess" property="parameterMap">
                  <c:out value="${subprocess.map.subprocessName}"/>
                </html-el:link>
              </logic:equal>
              <logic:equal name="subprocess" property="lastone" value="true">
                <html-el:link page="/bp/processSubprocessTaskView.do" name="subprocess" property="parameterTaskMap">
                  <c:out value="${subprocess.map.number}"/>:
                  <c:out value="${subprocess.map.friendlyName}"/>
                </html-el:link>
                &nbsp;<bean-el:message key="bp.process.taskSeparator" bundle="bp.messages"/>&nbsp;
                <c:out value="${subprocess.map.subprocessName}"/>
              </logic:equal>
            </logic:iterate>
          </div>
        </logic:greaterThan>
      </logic:present>
      <div class="pageSubtitle">
        <span class="left"><bean-el:message key="bp.process.processInstanceList.processInstances" bundle="bp.messages"/></span>
        <span class="right">&nbsp;</span>
      </div> 
      <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="Process instance list" width="100%">
        <tr class="listHeader">
          <th valign="top" id="processInstanceName" align="left">
            <bean-el:message key="bp.process.processInstanceList.header.processInstanceName" bundle="bp.messages"/>
          </th>
          <th valign="top" id="status" align="left">
            <bean-el:message key="header.status" bundle="bp.messages"/>
          </th>
          <th valign="top" id="startDate"  align="left">
            <bean-el:message key="bp.process.processInstanceList.header.startDate" bundle="bp.messages"/>
          </th>
          <th valign="top" id="dueDate" align="left">
            <bean-el:message key="bp.process.processInstanceList.header.dueDate" bundle="bp.messages"/>
          </th>
          <th valign="top" id="actions" align="left" nowrap>
            <bean-el:message key="header.actions" bundle="bp.messages"/>
          </th>
        </tr>
        <logic:empty name="processInstanceListView" property="instances">
          <tr class="listEven">
            <td align="center" colspan="5">
              <bean-el:message key="bp.process.processInstanceList.noProcessInstance" bundle="bp.messages"/>
            </td>
          </tr>
        </logic:empty>
        <logic:notEmpty name="processInstanceListView" property="instances">
          <logic:iterate id="instance" name="processInstanceListView" property="instances" indexId="i">
            <tr class="<%=i.intValue() % 2 == 0 ? "listEven" : "listOdd" %>">
              <td valign="top"  headers="processInstanceName">
                <html-el:link page="/bp/processInstance.do" paramId="instanceOid" paramName="instance" paramProperty="oid">
                  <c:out value="${instance.map.customId}"/>
                </html-el:link>
              </td>
              <td valign="top" headers="status">
                <logic:equal name="instance" property="enumStatus" value="0">
                  <bean-el:message key="bp.process.processInstance.state.0" bundle="bp.messages"/>
                </logic:equal>
                <logic:equal name="instance" property="enumStatus" value="1">
                  <bean-el:message key="bp.process.processInstance.state.1" bundle="bp.messages"/>
                </logic:equal>
                <logic:equal name="instance" property="enumStatus" value="2">
                  <bean-el:message key="bp.process.processInstance.state.2" bundle="bp.messages"/>
                </logic:equal>
                <logic:equal name="instance" property="enumStatus" value="3">
                  <bean-el:message key="bp.process.processInstance.state.3" bundle="bp.messages"/>
                </logic:equal>
              </td>
              <td valign="top" headers="startDate">
                <bean:write name="instance" property="startDate" formatKey="format.datetime.short" bundle="mc.messages"/>
              </td>
              <td valign="top" headers="dueDate">
                <bean:write name="instance" property="dueDate" formatKey="format.datetime.short" bundle="mc.messages"/>
              </td>
              <td valign="top" nowrap align="left" headers="actions">
                <logic:notEqual name="instance" property="enumStatus" value="2">
                  <logic:notEqual name="instance" property="enumStatus" value="3">
                    <a href='<%= response.encodeURL(request.getContextPath() + "/bp/processInstanceEditFromModel.do?action=edit&instanceOid=") %><c:out value="${instance.map.oid}"/>' ><c:out value='${editImg}' escapeXml="false"/></a>
                  </logic:notEqual>
                </logic:notEqual>
              </td>
            </tr>
          </logic:iterate>
        </logic:notEmpty>
      </table>
      <logic:notEmpty name="processInstanceListView" property="instances">
        <div class="pagination">
          <bean-el:message key="bp.common.pagination.show" bundle="bp.messages"/>
          <html-el:text name="processInstanceListForm" property="entriesPerPage" size="10" maxlength="10" onkeypress="return checkNumberOnly(event)"/>
          <bean-el:message key="bp.common.pagination.itemsPerPage" bundle="bp.messages"/>
          &nbsp;
          <html-el:submit property="go" styleClass="inputButton"
            onclick="return doGo('processInstanceListForm');">
            <bean-el:message key="button.goto" bundle="bp.messages"/>
          </html-el:submit>
          <logic:greaterThan name="processInstanceListForm" property="totalPageNumber" value="1">
            &nbsp;&nbsp;&nbsp;
            <logic:greaterThan name="processInstanceListForm" property="currentPageNumber" value="0">
              <logic:greaterThan name="processInstanceListForm" property="totalPageNumber" value="1">
                <logic:greaterThan name="processInstanceListForm" property="currentPageNumber" value="1">
                  <a href="#" class="paginationBtn" onclick="javascript:gotoFirstPage('processInstanceListForm')">
                    <bean-el:message key="bp.common.pagination.first" bundle="bp.messages"/></a>
                  <a href="#" class="paginationBtn" onclick="javascript:gotoPreviousPage('processInstanceListForm')">
                    <bean-el:message key="bp.common.pagination.previous" bundle="bp.messages"/></a>
                </logic:greaterThan>
              </logic:greaterThan>
              <bean:define id="currentPageNumberAttr" name="processInstanceListForm" property="currentPageNumber"/>
              <bean:define id="totalPageNumberAttr" name="processInstanceListForm" property="totalPageNumber"/>
              <% Integer currentPageNumberInt = (Integer)pageContext.getAttribute("currentPageNumberAttr"); %>
              <% Integer totalPageNumberInt = (Integer)pageContext.getAttribute("totalPageNumberAttr"); %>
              <bean-el:message key="bp.common.pagination.pageOf" 
                arg0="<%= currentPageNumberInt.toString() %>" 
                arg1="<%= totalPageNumberInt.toString() %>" bundle="bp.messages"/>
              <logic:greaterThan name="processInstanceListForm" property="totalPageNumber" value="1">
                <logic:equal name="processInstanceListForm" property="lastPage" value="false">
                  <a href="#" class="paginationBtn" onclick="javascript:gotoNextPage('processInstanceListForm')">
                    <bean-el:message key="bp.common.pagination.next" bundle="bp.messages"/></a>
                  <a href="#" class="paginationBtn" onclick="javascript:gotoLastPage('processInstanceListForm')">
                    <bean-el:message key="bp.common.pagination.last" bundle="bp.messages"/></a>
                </logic:equal>
              </logic:greaterThan>
            </logic:greaterThan>
          </logic:greaterThan>
        </div>
      </logic:notEmpty>
    </td>
  </tr>
</table>
</html-el:form>

